<?php
namespace BeautyParlorAdmin\Controller;

use Think\Controller;
use Common\Util\UploadHandler;

class ProductController extends BaseController {
    public function index(){
//     	layout('Layout/admin');
    	$ProductModel = D('Product');
    	
    	$list = $ProductModel->where(array('status' => 1))->select();
    	
    	$this->assign('list', $list);
    	$this->display();
    }
    
    public function add() {
    	if (!IS_POST) {
    		$this->display();
    	} else {
    		$ProductModel = D('Product');
    		if ($ProductModel->addProduct(I('post.'))) {
    			$this->success('添加成功');
    		} else {
    			$this->error($ProductModel->getError());
    		}
    	}
    }
    
    public function edit() {
    	if (!IS_POST) {
    		$id = I ('get.id');
    		$Model = D('Product');
    		
    		$item = $Model->getProductInfo($id);
    		$steps = D('Content')->getTargetContent($id);
    		
    		$this->assign('steps', $steps);
    		$this->assign('item', $item);
    		$this->display();
    	} else {
    		$id = I ('post.id');
    		
    		$ProductModel = D('Product');
    		if (!$ProductModel->updateInfo(I('post.'))) {
    			$this->error($ProductModel->getError());
    		} else {
    			$this->success('编辑成功');
    		}
    	}
    }
    
    public function delete() {
    	$id = I('get.id');
    	
    	$result = M('product')->where(array('id' => $id))->save(array('status' => 0));
    	if (!$result) {
    		$this->success('删除失败');
    	}
    	$this->error('删除成功');
    }
    
    public function uploadProductCover() {
    	$options = C('PRODUCT_FILE_UPLODE_CONFIG');
    	$options ['upload_url'] = get_full_url().'/Uploads/Product/';
    	$uploadHandler = new UploadHandler($options, false);
    	$result = $uploadHandler->post(false);
    	$param_name = $options ['param_name'];
    	$this->ajaxReturn($result [$param_name] [0]);
    }
}